Examples of awk built-in variables: Examples of NR, FNR, and NF, awkfnr
NR indicates the number of data reads Based on the record delimiter after running from awk. The default record delimiter is a line break. Therefore, the default value is the number of data rows read, NR
NR indicates the number of data rows read after the awk starts executing the program.
FNR is similar to Nr. The difference is that FNR accumulates from 0 every time awk opens a new file.
The following are two examples:
1. The output results for a single file NR and FNR are
Awk in the meaning of NF and nr, in fact you already know the meaning of NF and nr, NF is a text file in a row (a record) of the number of fields, NR represents the text file of the number of rows (records). It is often used in programming, especially when it comes to data processing. It is recommended that you look at
The book says:
NR, which represents the number of rows of data that awk reads after the program starts executing.
FNR, which is similar to NR, is different from the 0 FNR that awk accumulates every new file.
Here's a look at two examples:
1, same as the output result of a single file
One, the difference between NR and FNR
NR: Represents the current number of recordsFNR: Also represents the current number of records, but the scope of FNR is within only one file. If you reopen the file, FNR starts at 1.
Second, the example illustrates
1, test file AAA,CCCC
Copy Code code as follows:
[Root@blackghost test2]# Cat AAA//test File AAA
1111:23434:zhang
Hoadsf:asdf:ccc
awk supports processing records and fields, the processing of fields is not implemented by grep and SED, which is one of the reasons why awk is better than both. In awk, by default, a row in a text file is always regarded as a record, and a part of a row is used as a field in the record. To operate on these different fields,
record, and a part of a row is used as a field in the record. To operate on these different fields, awk uses the shell method to represent different fields in the row (record) in sequence in the form of $1, $2, $3. In particular, awk uses $0 to represent the entire row (record ). Different fields are separated by characters called delimiters. The default Delimiter is space.
part of a row is used as a field in the record. To operate on these different fields, awk uses the shell method to represent different fields in the row (record) in sequence in the form of $1, $2, $3. In particular, awk uses $0 to represent the entire row (record ). Different fields are separated by characters called delimiters. The default Delimiter is space. Awk
in a) print a[i],i| " Sort-k1-nr|head-n10 "} ' Access.logMethod 2:$ awk ' {print $} ' access.log |sort |uniq-c |sort-k1-nr |head-n10Description: a[$1]++ creates an array A, takes the first column as the subscript, uses the operator + + as the array element, and the element initial value is 0. When processing an IP, the subscript is IP, the element plus 1, proces
] 'begin{ initialization} Condition Type 1 {Action 1} Condition Type 2 {Action 2 }.... END {post-processing }'The statements in BEGIN and END play a role before and after reading the file (in_file). They can be understood as initialization and scanning.(1) parameter description:-F re: Allows awk to change its field separator-V var = $ v: Assign the value of v to var. If multiple variables need to be assigned a value, write multiple-v values. Each vari
, $3. In particular, awk uses $0 to represent the entire row (record ). Different fields are separated by characters called delimiters. The default Delimiter is space. Awk allows the use of-F in command lineTo change the separator. In fact, awk uses a built-in variable FS to remember this separator. Awk has several suc
command line-f optionNF browsing the number of fields recordedThe number of records that NR has readOFS Output Field SeparatorORS Output Record SeparatorRS Control Record Separator
In addition, the $ variable refers to the entire record. Represents the first field in the current row, and $ $ represents the second field of the current row,...... Analogy
Statistics/etc/passwd: File name, line number for each line, number of columns per row, correspon
, and a part of a row is used as a field in the record. In order to operate these different fields, awk uses the shell method to represent different fields in the row (record) in sequence in the form of 1, 2, 3. In particular, awk uses 0 to represent the entire row (record ). Different fields are separated by characters called delimiters. The default Delimiter is space.
Variables
Awk has many built-in variables used to set environment information. These variables can be changed. The following lists the most common variables.
Number of ARGC command line parameters
ARGV command line parameter arrangement
ENVIRON supports the use of system environment variables in the queue
FILENAME awk browsed file name
Number of FNR browsing file records
FS sets the input domain separator
Ulimit-n corresponding/etc/security/limits.conf file settingsProblem: Can ' t open so many files for Linux OPS students, I believe they have encountered this problem.Under Linux All resources are files, ordinary files are files, disk printers are files, sockets of course also files. The system is set to 1024 by default, but this number is not enough for servers, especially large concurrency, so we need to change the actual situation to a larger and more reasonable value.Modify methodTemporary so
Error Nr is reported at the last step of mysql installation. 1045 solve the problem that the first time mysql database was installed on my computer, it always cracked the crude Nr. 1045 1. go to control panel-> Administrative Tools-> services, open MySQL Services 2. enter the command window, and then enter the MySQL installation directory, for example, enter the c: \ Program Files \ MySQL Server 5.1 \ bin c
Source Address: http://yanshuaijun.2010.blog.163.com/blog/static/362411622011102443056225/ topic: MySQL Error nr.1045 workaround2011-11-24 16:30:56| Category: mysql| report | Font Size Subscribe1. Go to cmd manually stop MySQL service: net stop MySQL. 2. Modify the My.ini file under the 5.1\ directory of the C:\Program files\mysql\mysql Server and add it under [mysqld]Skip-grant-tables 3. Start the MySQL service in cmd: net start MySQL 4. Open MyS
" {while ("Ls-1rt" | getline) {print NR ":" $ > "List.txt";}} 'Guess: What is the content of List.txt after the script is finished running?Answer:Cat List.txt 1 : Demo.txt 1 : List.txtI believe a lot of friends will feel surprised:Some people think that List.txt should have only one row of data, which is the last line of the LS-1RT command output.Some people think there should be 6 data, because LS-1RT executes three times.People who have this idea,
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.